﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 布局练习
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        //当窗口加载的时候初始化页面并随机显示图片
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //创建一个 10*10的表格         
            for (int j = 0; j < 10; j++)
            {
                //创建行对象
                RowDefinition rd = new RowDefinition();
                //添加到表格
                pgrid.RowDefinitions.Add(rd);
                //创建列
                ColumnDefinition cd = new ColumnDefinition();
                //添加到表格
                pgrid.ColumnDefinitions.Add(cd);
            }


            //随机数对象
            Random random = new Random();

            //两层循环
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    //产生一个随机数[0,9)
                    int r = random.Next(1, 9);
                    Image img = new Image();//创建图片对象
                    img.Source = new BitmapImage(new Uri("images/" + r + ".jpg", UriKind.Relative));

                    //设置图片的行
                    Grid.SetRow(img, i);
                    //设置图片的列
                    Grid.SetColumn(img, j);
                    //将图片添加到表格中
                    pgrid.Children.Add(img);//将图片添加到表格中

                }
            }
        }
    }
}
